package com.example.teesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teesystem.common.cache.IssueCache;
import com.example.teesystem.common.cache.KnowledgeCache;
import com.example.teesystem.common.cache.UserCache;
import com.example.teesystem.entity.Chapter;
import com.example.teesystem.entity.Issue;
import com.example.teesystem.entity.Knowledge;
import com.example.teesystem.entity.vo.*;
import com.example.teesystem.entity.vo.chapter.*;
import com.example.teesystem.mapper.IssueMapper;
import com.example.teesystem.mapper.KnowledgeMapper;
import com.example.teesystem.service.ChapterService;
import com.example.teesystem.mapper.ChapterMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 28131
* @description 针对表【t_chapter】的数据库操作Service实现
* @createDate 2022-03-25 08:22:18
*/
@Service
@RequiredArgsConstructor
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter>
    implements ChapterService{
    private final IssueMapper issueMapper;
    private final KnowledgeMapper knowledgeMapper;
    
    @Override
    public boolean add(ChapterUploadVo chapterUploadVo) {
//        if(this.validChapterExist(chapterUploadVo.getCourseId(), chapterUploadVo.getChapter())) {
//            Chapter chapter = new Chapter();
//            BeanUtils.copyProperties(chapterUploadVo, chapter);
//            QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
//            return this.save(chapter);
//        }
        Chapter chapter = new Chapter();
        BeanUtils.copyProperties(chapterUploadVo, chapter);
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", chapterUploadVo.getCourseId());
        chapter.setChapter((int) this.count(wrapper) + 1);
        return this.save(chapter);
    }
    
    @Override
    public boolean update(ChapterUpdateVo chapterUpdateVo) {
        Chapter chapter = new Chapter();
        BeanUtils.copyProperties(chapterUpdateVo, chapter);
        return this.updateById(chapter);
    }
    
    @Override
    public PageResponseVo<ChapterResponseVo> getChapters(ChapterSelectVo chapterSelectVo) {
        Page<Chapter> chapterPage = new Page<>(chapterSelectVo.getPageConfig().getCur(), chapterSelectVo.getPageConfig().getSize());
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", chapterSelectVo.getCourseId());
        Page<Chapter> result = this.page(chapterPage, wrapper);
        List<ChapterResponseVo> responseVos = result.getRecords().stream()
                .map(chapter -> {
                    ChapterResponseVo chapterResponseVo = new ChapterResponseVo();
                    BeanUtils.copyProperties(chapter, chapterResponseVo);
                    chapterResponseVo.setUploadName(UserCache.userMap.getOrDefault(chapter.getUploadId(), UserCache.NONE_USER).getMyName());
                    chapterResponseVo.setUpdateName(UserCache.userMap.getOrDefault(chapter.getUpdateId(), UserCache.NONE_USER).getMyName());
                    return chapterResponseVo;
                })
                .collect(Collectors.toList());
        return new PageResponseVo<>(responseVos, result.getCurrent(), result.getSize(), result.getPages());
    }
    
//    @Override
//    public boolean addChapters(List<ChapterUploadVo> chapterUploadVos) {
//        List<Chapter> chapterList = chapterUploadVos.stream()
//                .filter(chapterUploadVo -> this.validChapterExist(chapterUploadVo.getCourseId(), chapterUploadVo.getChapter()))
//                .map(chapterUploadVo -> {
//                    Chapter chapter = new Chapter();
//                    BeanUtils.copyProperties(chapterUploadVo, chapter);
//                    return chapter;
//                })
//                .collect(Collectors.toList());
//        return this.saveBatch(chapterList);
//    }
    
    @Override
    public boolean delete(ChapterDeleteVo chapterDeleteVo) {
        QueryWrapper<Issue> issueQueryWrapper = new QueryWrapper<>();
        issueQueryWrapper.eq("chapter_id", chapterDeleteVo.getChapterId());
        QueryWrapper<Knowledge> knowledgeQueryWrapper = new QueryWrapper<>();
        knowledgeQueryWrapper.eq("chapter_id", chapterDeleteVo.getChapterId());
        List<Issue> issues = this.issueMapper.selectList(issueQueryWrapper);
        List<Knowledge> knowledges = this.knowledgeMapper.selectList(knowledgeQueryWrapper);
        
        // 首先判断是否级联删除
        if(!chapterDeleteVo.getCascadeDelete()) {
            if(!issues.isEmpty() || !knowledges.isEmpty()) {
                return false;
            }
            return this.removeById(chapterDeleteVo.getChapterId());
        }
        this.issueMapper.updateChapterIdByChapterId(0, chapterDeleteVo.getChapterId());
        this.knowledgeMapper.delete(knowledgeQueryWrapper);
        knowledges.forEach(knowledge -> KnowledgeCache.knowledgeMap.remove(knowledge.getId()));
        return this.removeById(chapterDeleteVo.getChapterId());
    }
}
